/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.sec;

import dp1.titandevelop.titano.persistent.Perfil;
import dp1.titandevelop.titano.persistent.Permiso;
import dp1.titandevelop.titano.service.PerfilService;
import dp1.titandevelop.titano.service.PermisoService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.view.LoginT;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author chrestean
 */
public class ModulosT_SEG_APerfilEdit extends javax.swing.JInternalFrame {

    public ArrayList<Permiso> listaPermisosPerfil;
    public ArrayList<Permiso> listaPermisosBD;
    Permiso p = new Permiso();
    Perfil perfilaux = new Perfil();
    private AbstractTableModel modeloPermisosBD = new AbstractTableModel() { // usando una clase anonima
        // colocando los titulos de las columnas
        String[] titles = {"Descripción Permiso", "Módulo", "Opcion"};

        public String getColumnName(int col) {
            return titles[col];
        }

        public int getRowCount() {
            return listaPermisosBD.size(); // cuantos registros (filas)
        }

        public int getColumnCount() {
            return 3; // cuantas columnas
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            String res = null;
            Permiso p = listaPermisosBD.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    res = p.getDescripcion();
                    break;
                case 1:
                    res = p.getModulo();
                    break;
                case 2:
                    res = p.getOpcion();
                    break;
                case 3:
                    res = String.valueOf(p.getIdpermiso());
                    break;

            }
            return res;
        }

        public Permiso getValue(int row) {
            return listaPermisosBD.get(row);
        }
    };
    private AbstractTableModel modeloPermisosPerfil = new AbstractTableModel() { // usando una clase anonima
        // colocando los titulos de las columnas
        String[] titles = {"Descripción Permiso", "Módulo", "Opcion"};

        public String getColumnName(int col) {
            return titles[col];
        }

        public int getRowCount() {
            return listaPermisosPerfil.size(); // cuantos registros (filas)
        }

        public int getColumnCount() {
            return 3; // cuantas columnas
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            String res = null;
            Permiso p = listaPermisosPerfil.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    res = p.getDescripcion();
                    break;
                case 1:
                    res = p.getModulo();
                    break;
                case 2:
                    res = p.getOpcion();
                    break;

            }
            return res;
        }
    };

    public ModulosT_SEG_APerfilEdit() {
        initComponents();

    }

    ModulosT_SEG_APerfilEdit(Perfil perfil) {
        PermisoService pe = new PermisoService();
        initComponents();
        perfilaux = perfil;
        this.listaPermisosPerfil = pe.buscarPermisosActivos(perfil.getIdperfil());
        this.tablaPermisosPerfil.setModel(modeloPermisosPerfil);
        this.txtIDPerfil.setVisible(false);
        this.listaPermisosBD = pe.buscarPermisosDistintos(perfil.getIdperfil());
        this.tablaPermisosBD.setModel(modeloPermisosBD);
         ValidacionService validacionService = new ValidacionService();
         validacionService.SLetras(this.txtPerfil);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblPerfil = new javax.swing.JLabel();
        txtPerfil = new javax.swing.JTextField();
        botonSalir = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JToggleButton();
        btnQuitar = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPermisosPerfil = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaPermisosBD = new javax.swing.JTable();

        setResizable(true);
        setTitle("Editar Perfil");
        setToolTipText("");
        setMaximumSize(new java.awt.Dimension(1100, 450));
        setMinimumSize(new java.awt.Dimension(1100, 450));
        setPreferredSize(new java.awt.Dimension(1100, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        lblPerfil.setText("Descripcion");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblPerfil)
                .addGap(10, 10, 10)
                .addComponent(txtPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtIDPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPerfil)
                    .addComponent(txtIDPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        botonSalir.setToolTipText("Salir");
        botonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSalirActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        botonGuardar.setToolTipText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Permisos"));
        jPanel2.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        btnAgregar.setText("<< Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnQuitar.setText("Quitar >>");
        btnQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Permisos Perfil"));

        tablaPermisosPerfil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Descripcion", "Modulo", "Opcion"
            }
        ));
        tablaPermisosPerfil.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaPermisosPerfilKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tablaPermisosPerfil);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Permisos Sistema"));

        tablaPermisosBD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Descripcion", "Modulo", "Opcion"
            }
        ));
        tablaPermisosBD.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaPermisosBDKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(tablaPermisosBD);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnAgregar)
                    .addComponent(btnQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(btnAgregar)
                        .addGap(62, 62, 62)
                        .addComponent(btnQuitar)
                        .addGap(0, 56, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botonGuardar)
                        .addGap(1, 1, 1)
                        .addComponent(botonSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 1069, Short.MAX_VALUE))))
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonGuardar)
                    .addComponent(botonSalir))
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSalirActionPerformed
        ModulosT_SEG_APerfil admUser = new ModulosT_SEG_APerfil();
        this.getParent().add(admUser);

        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admUser.getSize();
        admUser.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 4);

        admUser.show();
        this.dispose();
    }//GEN-LAST:event_botonSalirActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed


        this.listaPermisosPerfil.add(this.listaPermisosBD.get(this.tablaPermisosBD.getSelectedRow()));
        this.modeloPermisosPerfil.fireTableChanged(null);
        this.listaPermisosBD.remove(this.tablaPermisosBD.getSelectedRow());
        this.modeloPermisosBD.fireTableChanged(null);

    }//GEN-LAST:event_btnAgregarActionPerformed
//
    private void btnQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarActionPerformed
        this.listaPermisosBD.add(this.listaPermisosPerfil.get(this.tablaPermisosPerfil.getSelectedRow()));
        this.modeloPermisosBD.fireTableChanged(null);
        this.listaPermisosPerfil.remove(this.tablaPermisosPerfil.getSelectedRow());
        this.modeloPermisosPerfil.fireTableChanged(null);
    }//GEN-LAST:event_btnQuitarActionPerformed

    private void tablaPermisosBDKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaPermisosBDKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            this.btnAgregar.doClick();
        }
    }//GEN-LAST:event_tablaPermisosBDKeyPressed

    private void tablaPermisosPerfilKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaPermisosPerfilKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            this.btnQuitar.doClick();
        }
    }//GEN-LAST:event_tablaPermisosPerfilKeyPressed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        //ID 12
        PerfilService ps = new PerfilService();
        Integer i = 0;
        for (; i < LoginT.permisosxperfil.size(); i++) {
            if (12 == (Integer) LoginT.idspermisosxperfil.get(i)) {
                try {
                    ps.actualizarPerfil(this.perfilaux.getIdperfil(), listaPermisosPerfil);
                    ModulosT_SEG_APerfil admUser = new ModulosT_SEG_APerfil();
                    this.getParent().add(admUser);

                    Dimension desktopSize = this.getParent().getSize();
                    Dimension jInternalFrameSize = admUser.getSize();
                    admUser.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                            (desktopSize.height - jInternalFrameSize.height) / 4);
                    JOptionPane.showMessageDialog(this, "Se actualizó el perfil " + this.perfilaux.getDescripcion() + " con los datos ingresados",
                            "Creacion de Perfil",
                            JOptionPane.INFORMATION_MESSAGE);
                    admUser.show();
                    this.dispose();


                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la creación del perfil",
                            "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
                break;
            }
        }
        if (i >= LoginT.permisosxperfil.size()) {
            JOptionPane.showMessageDialog(this, "No puedes proceder, al parecer careces de permisos.",
                    "No posee permisos",
                    JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_botonGuardarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonSalir;
    private javax.swing.JToggleButton btnAgregar;
    private javax.swing.JToggleButton btnQuitar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblPerfil;
    private javax.swing.JTable tablaPermisosBD;
    private javax.swing.JTable tablaPermisosPerfil;
    public static final javax.swing.JTextField txtIDPerfil = new javax.swing.JTextField();
    public static javax.swing.JTextField txtPerfil;
    // End of variables declaration//GEN-END:variables
}
